/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.collablet.coop.recommend;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.org.groupwareworkbench.core.bd.GenericDAO;
import br.org.groupwareworkbench.core.bd.GenericEntity;

public class RecommendDAO extends GenericDAO<Recommend> {

    public RecommendDAO() {
        super(Recommend.class);
    }
  
    @SuppressWarnings("unchecked")
    public List<Recommend> listGenericEntityByNumRelated(long idInstance, GenericEntity genericEntity, int numRelated) {
        EntityManager em = getEntityManager();
        String queryString = "SELECT a FROM " + Recommend.class.getSimpleName() +
                            " a WHERE a.idInstance = :idInstance AND" +
                            " (a.genericEntityFrom = :genericEntity OR a.genericEntityTo = :genericEntity)" +
                            " ORDER BY a.similaritysProduct DESC";
        Query query = em.createQuery(queryString);
        query.setFirstResult(0);
        query.setMaxResults(numRelated);
        query.setParameter("genericEntity", genericEntity);
        query.setParameter("idInstance", idInstance);
        System.out.println("Numero de registros related : " + query.getResultList().size());
        return query.getResultList();
    }
}
